package com.itheima.ioc;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//JDK代理类
public class FirstProxy implements InvocationHandler{
    //声明目标类接口
    private UserDao userDao;
    //创建代理方法
    public Object createProxy(UserDao userDao){
        this.userDao = userDao;
        //.类加载器
        ClassLoader classLoader = FirstProxy.class.getClassLoader();
        //2.被代理对象实现的所有接口
        Class[] classes = userDao.getClass().getInterfaces();
        //3.使用代理类进行增强,返回的是代理对象
        return Proxy.newProxyInstance(classLoader,classes,this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //创建切面对象
        FirstAspect firstAspect = new FirstAspect();
        //前增强
        firstAspect.check_Permissions();
        Object obj = method.invoke(userDao,args);
        //后增强
        firstAspect.log();
        return obj;
    }
}
